package com.xiao.framework.ds.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.framework.ds.annotation.Database;
import com.xiao.framework.ds.example.bean.User;
import com.xiao.framework.ds.example.bean.Video;
import com.xiao.framework.ds.example.mapper.UserMapper;
import com.xiao.framework.ds.example.mapper.VideoMapper;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * @author <a href="mailto:xiaoquanidea@163.com">hutiquan</a>
 * @since 2019-08-25 18:43
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class MultiDataSourceService {

  @Autowired private UserMapper userMapper;

  @Autowired private VideoMapper videoMapper;

  @Autowired
  @Lazy
  private MultiDataSourceService service;

  @Database("item_master")
  public void itemMaster(){
    List<User> users = userMapper.selectList(new QueryWrapper<>());
    System.out.println("item_master = " + users);
    service.itemSlave();
  }

  @Database("item_slave")
  public void itemSlave(){
    List<User> users = userMapper.selectList(null);
    System.out.println("item_slave = " + users);
    service.liveMaster();
  }

  @Database("live_master")
  public void liveMaster(){
    List<Video> videos = videoMapper.selectList(null);
    System.out.println("live_master = " + videos);
    service.liveSlave();
  }

  @Database("live_slave")
  public void liveSlave(){
    List<Video> videos = videoMapper.selectList(null);
    System.out.println("live_slave = " + videos);
  }


  public void readItem(){
    List<User> users = userMapper.selectList(null);
//    List<User> users = userMapper.queryAll();
    users.forEach(System.out::println);
  }

  public void writeItem(){
    User user = new User();
    user.setUsername("xiaoxiao");
    user.setPhone("16637658030");
    user.setSalt("111111111");
    user.setPassword("123456");
    user.setCreated(LocalDateTime.now().toString());
    int count = userMapper.insert(user);
    System.out.println("新增" + count + "个");
  }
}
